from threading import Thread
import socket
from socket import socket
import msg_proto

class SendMsgThread(Thread):
    sock:socket = None
    bin_msg_list = []
    uid:int

    def __init__(self, sock):
        super().__init__()
        self.sock = sock
        self.uid = 0

    def set_uid(self, uid):
        self.uid = uid

    def send_msg(self, msg_id, content):
        print(f"[SEND_MSG] USER({self.uid}), msg_id:{msg_id} content:{content}")
        bin_msg = msg_proto.pack(msg_id, content)
        self.bin_msg_list.append(bin_msg)    

    def run(self):
        running = True
        while(running):
            if len(self.bin_msg_list) > 0:
                for bin_msg in self.bin_msg_list:
                    self.sock.send(bin_msg)
                self.bin_msg_list = []               
